package com.lyb269.thread;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;

/**
 * Executors创建的四种线程池
 */
public class ExecutorsCreateThreadTest {

    @Test
    public void testCachedThreadPoll(){
        //创建一个可缓存线程池,灵活回收
        ExecutorService service = Executors.newCachedThreadPool();
        Thread t = new Thread(() -> {
            Thread current = Thread.currentThread();
            System.out.println(current.getName()+" running");
        });
        for (int i = 0; i < 10; i++){
            service.execute(t);
        }
        //打印如下
        //pool-1-thread-1 running
        //pool-1-thread-2 running
        //pool-1-thread-4 running
        //pool-1-thread-3 running
        //pool-1-thread-5 running
        //pool-1-thread-1 running   thread-1回收再用
        //pool-1-thread-5 running   thread-5回收再用
        //pool-1-thread-6 running
        //pool-1-thread-7 running
        //pool-1-thread-8 running

    }

    @Test
    public void testSingleThreadPool(){
        //创建一个单线程的线程池,所有的任务FIFO/LIFO
        ExecutorService service = Executors.newSingleThreadExecutor();
        Thread t = new Thread(() -> {
            Thread current = Thread.currentThread();
            System.out.println(current.getName()+" running");
        });
        for (int i = 0; i < 10; i++){
            service.execute(t);
        }
        //打印如下:
        //pool-1-thread-1 running
        //pool-1-thread-1 running
        //pool-1-thread-1 running
        //pool-1-thread-1 running
        //pool-1-thread-1 running
        //pool-1-thread-1 running
        //pool-1-thread-1 running
        //pool-1-thread-1 running
        //pool-1-thread-1 running
        //pool-1-thread-1 running

        //始终只有 thread-1在执行
    }

    @Test
    public void testFixedThreadPool(){
        //创建一个定长的线程池
        ExecutorService service = Executors.newFixedThreadPool(2);
        Thread t = new Thread(() -> {
            Thread current = Thread.currentThread();
            System.out.println(current.getName()+" running");
        });
        for (int i = 0; i < 10; i++){
            service.execute(t);
        }

        //打印如下:
        //pool-1-thread-2 running
        //pool-1-thread-1 running
        //pool-1-thread-2 running
        //pool-1-thread-1 running
        //pool-1-thread-2 running
        //pool-1-thread-1 running
        //pool-1-thread-2 running
        //pool-1-thread-1 running
        //pool-1-thread-2 running
        //pool-1-thread-1 running
        //始终只有thread-1,thread-2两个线程在跑,谁先释放谁来
    }

    @Test
    public void testScheduledThreadPool(){
        //创建一个定长的线程池,支持定时及周期任务
        ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Thread t = new Thread(() -> {
            Thread current = Thread.currentThread();
            System.out.println(current.getName()+" running;执行时间:"+sdf.format(new Date()));
        });

        //延迟2秒执行
        System.out.println("当前时间:"+sdf.format(new Date()));
        service.schedule(t,2, TimeUnit.SECONDS);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //马上执行(延迟0秒)后,每隔两秒执行一次
        System.out.println("当前时间:"+sdf.format(new Date()));
        service.scheduleAtFixedRate(t, 0L, 2L, TimeUnit.SECONDS);

        //打印如下:
        //当前时间:2020-03-03 21:09:21
        //pool-1-thread-1 running;执行时间:2020-03-03 21:09:23
        //当前时间:2020-03-03 21:09:24
        //pool-1-thread-1 running;执行时间:2020-03-03 21:09:24
        //pool-1-thread-1 running;执行时间:2020-03-03 21:09:26
        //pool-1-thread-1 running;执行时间:2020-03-03 21:09:28
        // .....隔两秒执行一次,一直打印下去....


    }
}
